home *** CD-ROM | disk | FTP | other *** search
/ Programming Windows (5th Edition) / Programming Windows, 5th ed. - Companion CD (097-0002183)(1999).iso / Chap08 / Beeper1 / Beeper1.c next >
Encoding:
C/C++ Source or Header  |  1998-10-09  |  2.8 KB  |  93 lines

  1. /*-----------------------------------------
  2.    BEEPER1.C  -- Timer Demo Program No. 1
  3.                  (c) Charles Petzold, 1998
  4.   -----------------------------------------*/
  5.  
  6. #include <windows.h>
  7.  
  8. #define ID_TIMER    1
  9.  
  10. LRESULT CALLBACK WndProc (HWND, UINT, WPARAM, LPARAM) ;
  11.  
  12. int WINAPI WinMain (HINSTANCE hInstance, HINSTANCE hPrevInstance,
  13.                     PSTR szCmdLine, int iCmdShow)
  14. {
  15.      static TCHAR szAppName[] = TEXT ("Beeper1") ;
  16.      HWND         hwnd ;
  17.      MSG          msg ;
  18.      WNDCLASS     wndclass ;
  19.      
  20.      wndclass.style         = CS_HREDRAW | CS_VREDRAW ;
  21.      wndclass.lpfnWndProc   = WndProc ;
  22.      wndclass.cbClsExtra    = 0 ;
  23.      wndclass.cbWndExtra    = 0 ;
  24.      wndclass.hInstance     = hInstance ;
  25.      wndclass.hIcon         = LoadIcon (NULL, IDI_APPLICATION) ;
  26.      wndclass.hCursor       = LoadCursor (NULL, IDC_ARROW) ;
  27.      wndclass.hbrBackground = (HBRUSH) GetStockObject (WHITE_BRUSH) ;
  28.      wndclass.lpszMenuName  = NULL ;
  29.      wndclass.lpszClassName = szAppName ;
  30.      
  31.      if (!RegisterClass (&wndclass))
  32.      {
  33.           MessageBox (NULL, TEXT ("Program requires Windows NT!"), 
  34.                       szAppName, MB_ICONERROR) ;
  35.           return 0 ;
  36.      }
  37.      
  38.      hwnd = CreateWindow (szAppName, TEXT ("Beeper1 Timer Demo"),
  39.                           WS_OVERLAPPEDWINDOW,
  40.                           CW_USEDEFAULT, CW_USEDEFAULT,
  41.                           CW_USEDEFAULT, CW_USEDEFAULT,
  42.                           NULL, NULL, hInstance, NULL) ;
  43.           
  44.      ShowWindow (hwnd, iCmdShow) ;
  45.      UpdateWindow (hwnd) ;
  46.           
  47.      while (GetMessage (&msg, NULL, 0, 0))
  48.      {
  49.           TranslateMessage (&msg) ;
  50.           DispatchMessage (&msg) ;
  51.      }
  52.      return msg.wParam ;
  53. }
  54.  
  55. LRESULT CALLBACK WndProc (HWND hwnd, UINT message, WPARAM wParam, LPARAM lParam)
  56. {
  57.      static BOOL fFlipFlop = FALSE ;
  58.      HBRUSH      hBrush ;
  59.      HDC         hdc ;
  60.      PAINTSTRUCT ps ;
  61.      RECT        rc ;
  62.      
  63.      switch (message)
  64.      {
  65.      case WM_CREATE:
  66.           SetTimer (hwnd, ID_TIMER, 1000, NULL) ;
  67.           return 0 ;
  68.  
  69.      case WM_TIMER :
  70.           MessageBeep (-1) ;          
  71.           fFlipFlop = !fFlipFlop ;
  72.           InvalidateRect (hwnd, NULL, FALSE) ;
  73.           return 0 ;
  74.           
  75.      case WM_PAINT :
  76.           hdc = BeginPaint (hwnd, &ps) ;
  77.           
  78.           GetClientRect (hwnd, &rc) ;
  79.           hBrush = CreateSolidBrush (fFlipFlop ? RGB(255,0,0) : RGB(0,0,255)) ;
  80.           FillRect (hdc, &rc, hBrush) ;
  81.  
  82.           EndPaint (hwnd, &ps) ;
  83.           DeleteObject (hBrush) ;
  84.           return 0 ;
  85.           
  86.      case WM_DESTROY :
  87.           KillTimer (hwnd, ID_TIMER) ;
  88.           PostQuitMessage (0) ;
  89.           return 0 ;
  90.      }
  91.      return DefWindowProc (hwnd, message, wParam, lParam) ;
  92. }
  93.